#include "ParseUsage.h"
#include "Mysocket.h"
#include "Myuser.h"
//#include "proto.h"
using std::vector;

void sig_alrm(int signo){
//    puts("10s l ");
    HeadMsg.cmd = HEART_BEAT;
    sprintf(buf, "%s", myUID);
    HeadMsg.length = strlen(buf) + 1;
    WriteMsg(sockfd, &HeadMsg, buf);
//    ReadMsg(sockfd, &HeadMsg, buf);
//    printf("head:\n\tcmd = %x\ncontent = %s\n", HeadMsg.cmd, buf);
    alarm(10);
}

void usage(){
    printf("Usage:\n");
    printf("Command Type:\"REGISTER\",\"LOGIN\", \"EXIT\"\n");

}

void ParseCommand1(int port,char * IP){
    
    int n;
	char nickname[LEN];
	char password[LEN];
    pthread_t ltid, ptid;
    SAI addr;
    socklen_t len;
    char *uIP;
    
    usage();
    char Command[LEN];
    gets(Command);
	/*----------------------------EXIT-------------------------*/
	if(strcmp(Command,"EXIT")==0 || strcmp(Command, "e") == 0)
	{
        printf("EXIT ing...\n");
		exit(0);
	}
	/*--------------------------REGISTER------------------------*/
	else if(strcmp(Command,"REGISTER")==0 || strcmp(Command, "r") == 0)
	{   
        sockfd = init(port,IP, &sockAddr);
        printf("Please put in your nickname and password\n");
        printf("nickname:");
        gets(nickname);
        printf("password:");
        gets(password);
        sprintf(buf,"%s %s",nickname,password);
        HeadMsg.cmd = REGISTER;
        HeadMsg.length = strlen(buf)+1;
    	WriteMsg(sockfd, &HeadMsg, buf);
        ReadMsg(sockfd, &HeadMsg, buf);
                
        printf("Your UID:%s\n",buf);
//        ParseCommand1( port, IP);
        exit(0);
    }
    /*--------------------------LOGIN------------------------*/
    else if(strcmp(Command,"LOGIN") == 0 || strcmp(Command, "l") == 0){
	    HeadMsg.cmd = LOGIN;
        sockfd = init(port,IP, &sockAddr);
        printf("LOGIN\n");
        printf("Please put in your UID and password\n");
        printf("UID:");
        gets(myUID);
        printf("password:");
        gets(password);
        
        len = sizeof(addr);
        getsockname(sockfd, (SA *) &addr, &len);
        uIP = inet_ntoa(addr.sin_addr);
        
        sprintf(buf,"%s %s %s",myUID,password, uIP);
        HeadMsg.length = strlen(buf)+1;

    	WriteMsg(sockfd, &HeadMsg, buf);
        ReadMsg(sockfd, &HeadMsg, buf);

        if (HeadMsg.error){
            printf("LOGIN ERROR @-@\n");           
        } else{
            printf("LOGIN SUCCESS^_^\n");
            HeadMsg.cmd = FRIEND_LIST;
            sprintf(buf, "%s", myUID);
            HeadMsg.length = strlen(buf) + 1;
            WriteMsg(sockfd, &HeadMsg, buf);
            ReadMsg(sockfd, &HeadMsg, buf);
            printf("%s", buf);
		    flag = 'N';
            pthread_create(&ltid, 0, Listen, 0);
            pthread_create(&ptid, 0, p2pFunc, (void *) &sockfd);
            userList.clear();
            alarm(30);
        }
        
    }
    else{
        perror("wrong command");
        ParseCommand1( port, IP);
    }    
}

